package com.ssps.front.rent.service.impl;

import com.ssps.entity.UserInfo;
import com.ssps.entity.UserInfoExample;
import com.ssps.front.rent.service.LessorUserInfoService;
import com.ssps.front.rent.service.LessorUserOrderService;
import com.ssps.mapper.UserInfoMapper;
import com.ssps.utils.AuthService;
import com.ssps.utils.Base64Util;
import com.ssps.utils.FileUtil;
import com.ssps.utils.HttpUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import java.awt.*;
import java.net.URLEncoder;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @author lg
 * @time 2020/07/27 13:38:53
 */
@Service
public class LessorUserInfoServiceImpl implements LessorUserInfoService {
    @Autowired
    UserInfoMapper userInfoMapper;


    @Override
    public void update(UserInfo userInfo) {
        userInfoMapper.updateByPrimaryKey (userInfo);
    }

    @Override
    public UserInfo findByUid(Integer uiid) {
        UserInfo userInfo = userInfoMapper.selectByPrimaryKey (uiid);

        return userInfo;

    }

    @Override
    public void uploadIdCard(String s, String s1, Integer uid) {

        UserInfo userInfo = userInfoMapper.selectByPrimaryKey (uid);
        if (userInfo == null) {
            userInfo.setIdCardBack (s1);
            userInfo.setIdCardFront (s);
            userInfo.setUid (uid);
            userInfo.setStatus ("待审核");
            userInfoMapper.updateByPrimaryKey (userInfo);
        }


    }

    @Override
    public String idcard(String url) {
        String ul = "https://aip.baidubce.com/rest/2.0/ocr/v1/idcard";
        try {
            // 本地文件路径
            String filePath = "C:\\Users\\刘歌\\Desktop\\upload\\" + url;

            byte[] imgData = FileUtil.readFileByBytes (filePath);
            String imgStr = Base64Util.encode (imgData);
            String imgParam = URLEncoder.encode (imgStr, "UTF-8");
            String param = "id_card_side=" + "front" + "&image=" + imgParam;
            // 注意这里仅为了简化编码每一次请求都去获取access_token，线上环境access_token有过期时间， 客户端可自行缓存，过期后重新获取。
            String accessToken = AuthService.getAuth ();
            String result = HttpUtil.post (ul, accessToken, param);
            /* System.out.println (result.substring (473, 491));*/
            return result;
        } catch (Exception e) {
            e.printStackTrace ();
        }
        return null;
    }

    @Override
    public void add(String address, String birthday, String name, String identity, Integer uid,String frontName,String backName) {

            String birthday1=birthday.substring (0,4);

            String birthday2=birthday.substring (4,6);

            String birthday3=birthday.substring (6);
            String birthday4=birthday1+"-"+birthday2+"-"+birthday3;

        UserInfoExample userInfoExample=new UserInfoExample ();
        UserInfoExample.Criteria criteria = userInfoExample.createCriteria ();
        criteria.andUidEqualTo (uid);

        List<UserInfo> userInfos = userInfoMapper.selectByExample (userInfoExample);

        SimpleDateFormat sdf = new SimpleDateFormat ("yyyy-MM-dd");
        Date dateBirthday = null;
        try {
            dateBirthday = sdf.parse (birthday4);
        } catch (ParseException e) {
            e.printStackTrace ();
        }
             userInfos.get (0).setAddress (address);
             userInfos.get (0).setBirthday (dateBirthday);
             userInfos.get (0).setUname (name);
             userInfos.get (0).setIdCard (identity);
             userInfos.get (0).setCid (1);
             userInfos.get (0).setStatus ("待审核");
             userInfos.get (0).setIdCardFront (frontName);
             userInfos.get (0).setIdCardBack (backName);
             userInfoMapper.updateByPrimaryKey (userInfos.get (0));
        }

    @Override
    public UserInfo findAll(Integer uiid) {
        UserInfo userInfo = userInfoMapper.selectByPrimaryKey (uiid);

        return userInfo;
    }

    @Override
    public void updateUserInfo(UserInfo loginUserInfo) {
            userInfoMapper.updateByPrimaryKey (loginUserInfo);
    }

    @Override
    public void referralCode(UserInfo loginUserInfo) {
        userInfoMapper.updateByPrimaryKey (loginUserInfo);
    }


}







